import pandas as pd
import os
import shutil
import zipfile

# 读取 CSV 文件
df = pd.read_csv('./chinanews_news.csv')

# 创建存放 HTML 文件的文件夹
html_folder = './news_html'
if not os.path.exists(html_folder):
    os.makedirs(html_folder)

# 生成新闻列表页面
index_html_path = os.path.join(html_folder, 'index.html')
with open(index_html_path, 'w', encoding='utf-8') as index_file:
    index_file.write('<html lang="zh-CN">\n<head>\n<meta charset="UTF-8">\n')
    index_file.write('<meta name="viewport" content="width=device-width, initial-scale=1.0">\n')
    index_file.write('<title>新闻列表</title>\n<style>\n')
    index_file.write('body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f4; }\n')
    index_file.write('header { background-color: #333; color: white; text-align: center; padding: 20px; }\n')
    index_file.write('section { width: 90%; margin: 20px auto; background-color: white; padding: 20px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); }\n')
    index_file.write('article { border-bottom: 1px solid #ccc; padding-bottom: 20px; margin-bottom: 20px; }\n')
    index_file.write('h2 { margin-top: 0; font-size: 1.2em; }\n')
    index_file.write('a { color: #333; text-decoration: none; }\n')
    index_file.write('a:hover { color: #007BFF; }\n')
    index_file.write('p { margin: 5px 0; font-size: 0.9em; }\n')
    index_file.write('@media (min-width: 768px) {\n')
    index_file.write('  section { width: 70%; }\n')
    index_file.write('  h2 { font-size: 1.4em; }\n')
    index_file.write('  p { font-size: 1em; }\n')
    index_file.write('}\n')
    index_file.write('</style>\n</head>\n<body>\n<header>\n<h1>新闻列表</h1>\n</header>\n<section>\n')

    for idx, row in df.iterrows():
        news_title = row['title']
        news_source = row['source']
        publish_time = row['publish_time']
        news_link = row['link']
        news_content = row['news_content']

        detail_page_name = f'news_{idx + 1}.html'
        index_file.write(f'<article>\n<h2><a href="{detail_page_name}">{news_title}</a></h2>\n')
        index_file.write(f'<p>发布时间：{publish_time}</p>\n')
        index_file.write(f'<p>新闻来源：{news_source}</p>\n')
        index_file.write(f'<p>原新闻链接：<a href="{news_link}" target="_blank">{news_link}</a></p>\n')
        index_file.write('</article>\n')

        # 生成新闻详情页面
        detail_html_path = os.path.join(html_folder, detail_page_name)
        with open(detail_html_path, 'w', encoding='utf-8') as detail_file:
            detail_file.write(f'<html lang="zh-CN">\n<head>\n<meta charset="UTF-8">\n')
            detail_file.write('<meta name="viewport" content="width=device-width, initial-scale=1.0">\n')
            detail_file.write(f'<title>{news_title}</title>\n<style>\n')
            detail_file.write('body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f4f4f4; }\n')
            detail_file.write('header { background-color: #333; color: white; text-align: center; padding: 20px; }\n')
            detail_file.write('section { width: 90%; margin: 20px auto; background-color: white; padding: 20px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); }\n')
            detail_file.write('h1 { margin-top: 0; font-size: 1.3em; }\n')
            detail_file.write('p { margin: 5px 0; line-height: 1.6; font-size: 0.9em; }\n')
            detail_file.write('@media (min-width: 768px) {\n')
            detail_file.write('  section { width: 70%; }\n')
            detail_file.write('  h1 { font-size: 1.6em; }\n')
            detail_file.write('  p { font-size: 1em; }\n')
            detail_file.write('}\n')
            detail_file.write(f'</style>\n</head>\n<body>\n<header>\n<h1>{news_title}</h1>\n</header>\n<section>\n')
            detail_file.write(f'<p>发布时间：{publish_time}</p>\n')
            detail_file.write(f'<p>新闻来源：{news_source}</p>\n')
            detail_file.write(f'<p>原新闻链接：<a href="{news_link}" target="_blank">{news_link}</a></p>\n')
            # 修正此处，将 {news_title} 替换为实际的新闻标题
            detail_file.write(f'<p>{news_content}</p>\n')
            detail_file.write('</section>\n</body>\n</html>')

    index_file.write('</section>\n</body>\n</html>')

# 创建 ZIP 文件
zip_file_path = './news_html.zip'
with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
    for root, dirs, files in os.walk(html_folder):
        for file in files:
            file_path = os.path.join(root, file)
            arcname = os.path.relpath(file_path, html_folder)
            zipf.write(file_path, arcname)

# 清理临时文件夹
shutil.rmtree(html_folder)